/*
 * This project provides a number of implementations for indexing and evaluating
 * <i>Continuous Top-k Queries over Text Streams</i>
 * and has been developped as part of the work described in the article:
 * 
 * Vouzoukidou, N., Amann, B., & Christophides, V. (2012, October). 
 * Processing continuous text queries featuring non-homogeneous scoring functions.
 * In Proceedings of the 21st ACM international conference on Information and 
 * knowledge management (pp. 1065-1074). ACM.
 * http://dl.acm.org/citation.cfm?id=2398404
 * 
 * This software is distributed under the GNU General Public License and as an
 * additional condition of usage, please cite the aforementioned article.
 * 
 * Copyright (C) 2012 ICS/FORTH, Greece and 
 *               2012 LIP6, France
 * 
 * Author: Vouzoukidou Nelly, email: nelly.vouzoukidou (at) gmail (dot) com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package gr.forth.ics.continuoustopk.datasets;

import gr.forth.ics.continuoustopk.Constants;
import gr.forth.ics.continuoustopk.Item;
import gr.forth.ics.continuoustopk.Query;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

/**
 *
 * @author Vouzoukidou Nelly, email: vuzukid (at) gmail (dot) com
 */
public class Datasets {
    static double[] getWeights(int length) {
        double[] weights = new double[length];
        double remaining = 1;
        for (int i = 0; i < weights.length; i++) {
            weights[i] = Constants.random.nextDouble() * remaining;
            remaining -= weights[i];
        }
        weights[weights.length - 1] += remaining;
        return weights;
    }

    // creates a *set* of terms of size length
    static String[] terms(int length, int vocabularySize) {
        if (vocabularySize < length) {
            throw new IllegalArgumentException("Found length " + length +
                    " and vocabulary size " + vocabularySize);
        }
        String[] terms = new String[length];
        out:
        for (int j = 0; j < terms.length; j++) {
            terms[j] = "t" + Constants.random.nextInt(vocabularySize);
            for (int m = 0; m < j; m++) {
                if (terms[j].equals(terms[m])) {
                    j--;
                    continue out;
                }
            }
        }
        return terms;
    }
    
    public static List<Query> queryList(QueriesIterator queries, int queryCount) {
        return toList(queries, queryCount);
    }
    
    public static List<Item> itemList(ItemsIterator items, int itemCount) {
        return toList(items, itemCount);
    }
    
    private static <E> List<E> toList(Iterator<E> elements, int size) {
        List<E> queryList = new ArrayList<E>();
        for (int i = 0; i < size; i++) {
            E element = elements.next();
            queryList.add(element);
        }
        return queryList;
    }
    
    // Default
    public static List<Item> defaultItems() throws FileNotFoundException {
        Constants.random = new Random(0);
        return Datasets.itemList(ItemsIterator.pressItems(), DatasetConstants.itemCount);
    }

    public static List<Query> defaultQueries() throws FileNotFoundException {
        Constants.random = new Random(0);
        return Datasets.queryList(QueriesIterator.mixedQueries(), DatasetConstants.queryCount);
    }

    // With given length
    public static List<Item> itemsWithLength(int size) throws FileNotFoundException {
        Constants.random = new Random(0);
        return Datasets.itemList(ItemsIterator.pressItems(size), DatasetConstants.itemCount);        
    }
    
    public static List<Query> queriesWithLength(int size) throws FileNotFoundException {
        Constants.random = new Random(0);
        return Datasets.queryList(QueriesIterator.queriesWithLength(size), DatasetConstants.queryCount);
    }
    
    // Generated data
    public static List<Item> generatedItems() {
        Constants.random = new Random(0);
        return Datasets.itemList(ItemsIterator.generatedItems(), DatasetConstants.itemCount);
    } 
    
    public static List<Query> generatedQueries() {
        Constants.random = new Random(0);
        return Datasets.queryList(QueriesIterator.generatedQueries(), DatasetConstants.itemCount);
    }
}
